﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using Converter;
using Csla.Xaml;
using Accounting.Library;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_DieuChinh
{
    public partial class TSCD_DieuChinh_ListView : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members

        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            // no parameters
        }

        public string Title
        {
            get { return "MuaHangHoa List"; }
        }

        #endregion

        #region Load Methods
        public TSCD_DieuChinh_ListView()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_DieuChinh.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);

            InitializeComponent();

            this.TSCD_DieuChinh_GridView.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource TSCD_DieuChinh_ListCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_DieuChinh_ListViewModelSource"];
                TSCD_DieuChinh_ListViewModel TSCD_DieuChinh_ListViewModel = new TSCD_DieuChinh_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                TSCD_DieuChinh_ListCollectionViewSource.Source = new List<object> { TSCD_DieuChinh_ListViewModel };
            }
        }
        #endregion

        #region View Model Related Methods
        private void AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            LoadOrderMainWindow(ParentChild1LoadMainWindowMode.New);
        }

        private void EditItemButton_Click(object sender, RoutedEventArgs e)
        {
            LoadOrderMainWindow(ParentChild1LoadMainWindowMode.Edit);
        }
       
        private void OnCellDoubleClick(object sender, RoutedEventArgs args)
        {
            LoadOrderMainWindow(ParentChild1LoadMainWindowMode.Edit);
        }

        // Load order main window
        private void LoadOrderMainWindow(ParentChild1LoadMainWindowMode mode) 
        {
            if (mode == ParentChild1LoadMainWindowMode.Edit)
            {
                if (this.TSCD_DieuChinh_GridView.SelectedItem != null)
                {
                    string parameter = ((TSCD_DieuChinhInfo)this.TSCD_DieuChinh_GridView.SelectedItem).Khoa.ToString();
                    Navigator.Current.Navigate("csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_DieuChinh.TSCD_DieuChinh_MainWindow,csla4Skeleton.UI.SL", parameter);
                }
            }
            else if (mode == ParentChild1LoadMainWindowMode.New) 
            {
                Navigator.Current.Navigate("csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_DieuChinh.TSCD_DieuChinh_MainWindow,csla4Skeleton.UI.SL");
            }
        }
        #endregion

    }
}
